//
//  CBJNetworkTools.swift
//  Weibo
//
//  Created by 崔健 on 16/5/22.
//  Copyright © 2016年 cbj. All rights reserved.
//

import UIKit
import AFNetworking

enum HTTPMethodType : Int{
    case GET
    case POST
}

struct API {
    
    static let host = "https://api.weibo.com/"
    
    //加载用户token
    static let loadAccessToken = "oauth2/access_token"
    //加载用户信息
    static let loadUserInfo = "2/users/show.json"
    static let loadHomeData = "2/statuses/home_timeline.json"
    static let sendStatues = "2/statuses/update.json"
}



class CBJNetworkTools: AFHTTPSessionManager {

    
    static var sharedInstance : CBJNetworkTools = {
    
        let url = NSURL(string: API.host)
        let tool = CBJNetworkTools(baseURL: url)
        
        tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tool
    }()
    
    func request(type : HTTPMethodType , urlString : String,parameters: AnyObject?, finished : (responseObject : AnyObject? , error : NSError?) -> ()) {
        
        //定义闭包参数 将相同的闭包当做参数传递
        let success = { (task: NSURLSessionDataTask, result: AnyObject?) -> () in
            //执行请求成功的回调
            finished(responseObject: result, error: nil)
        }
        
        let failure = { (task: NSURLSessionDataTask?, error: NSError) -> () in
            //执行失败的回调
            finished(responseObject: nil, error: error)
        }
        
        
        if type == .GET {
            
            
            GET(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }else{
        
            POST(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
        
    }
    
}


// 账号相关
extension CBJNetworkTools{

    func loadAccessToken(code: String , finished : (responseObject: AnyObject?, error: NSError?) -> ()) -> (){
    
        let parameter = ["client_id" : client_id,"client_secret" :client_secret , "grant_type" :"authorization_code" , "code" : code , "redirect_uri" : redirect_uri]
        
        request(.POST, urlString: API.loadAccessToken, parameters: parameter, finished: finished)
        
    }
    
    func loadUserInfo(parameter: [String: AnyObject],finished : (responseObject: AnyObject?, error: NSError?) -> ())  -> () {

        let token = parameter["access_token"]!
        let uid = parameter["uid"]!
        let parameters = ["access_token" : token , "uid" : uid]
        
        request(.GET, urlString: API.loadUserInfo, parameters: parameters) { (responseObject, error) in
            
            finished(responseObject: responseObject,error: error)
        }
        
    }
    
}

extension CBJNetworkTools{ // 发布微博

    func sendStatues(statues : String , finished:((responseObject: AnyObject?, error: NSError?) -> ())){
    
        guard let token = CBJUserAccountViewModel.sharedAccountViewModel.userAccount?.access_token else{
        
            return
        }
        
        let paramaters = ["access_token" : token , "status" : statues]
        request(.POST, urlString: API.sendStatues, parameters: paramaters) { (responseObject, error) in
            
            finished(responseObject: responseObject,error: error)
        }
    }
}


